package gwt.client.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.xml.client.Document;

import java.awt.*;


/**
 * Created by Sam on 22/03/14.
 */
public class MenuBtnWidget extends Composite implements HasMouseOverHandlers, HasClickHandlers {

    private Grid grid;
    private Label label;
    private Image img;
    public String name;

    public MenuBtnWidget(String text, String colour, String icon) {

        grid = new Grid(1,2);

        initWidget(grid);

        //DOM.setElementAttribute(getElement(), "id", "my-button-id");

        name = text;

        img = new Image(GWT.getModuleBaseURL()+"../images/"+icon);
        img.setSize("50px","50px");

        label = new Label(text);
        label.setText(text);
        label.setWidth("100pc");

        grid.setWidget(0,0,img);
        grid.setWidget(0, 1, label);
        grid.setSize("500px", "65px");
        grid.getCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_MIDDLE);

        /**
         * Set the id for widget, retrieve its DOM Element
         * and then set the id attribute.
         * This allows you to reference a specific widget in CSS:
         * #menu-button-id { font-size: 100%; }
         */

        //getElement().getStyle().setProperty("background", colour);
        getElement().setAttribute("id", "menu-button-id");


        //getElement().getStyle().
        //getElement().getStyle().setCursor(Cursor.HAND_CURSOR);

        /*
        grid.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                HTMLTable.Cell cell = grid.getCellForEvent(event);
                GWT.log(name);
                GWT.log("MenuBtnWidget getSource() " + event.getSource()); // Grid Widget
                GWT.log("MenuBtnWidget toString() " + cell.toString());
            }
        });
        */

        //grid.sinkEvents(Event.ONCLICK);
    }

    public void onBrowserEvent(Event event) {
        super.onBrowserEvent(event);
        switch(event.getTypeInt()) {
            case Event.ONCLICK:
                GWT.log("MenuBtnWidget.onBrowserEvent() Event.ONCLICK");
        }
    }

    @Override
    // Handle events externally to this Widget, i.e. NavigationPanel (no sinkEvents()).
    // Implementing HasXXXHandler (in this case HasMouseOverHandlers)
    // requires us to override addXXXHandler (in this case addMouseOverHandler)
    // which must call inherited addDOMHandler method.
    // Here we delegate event handling to Widget.
    public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
        return addDomHandler(handler, MouseOverEvent.getType());
    }

    @Override
    public HandlerRegistration addClickHandler(ClickHandler handler) {
        //return grid.addClickHandler(handler);
        return addDomHandler(handler, ClickEvent.getType());
    }
}
